function pulseTrace = pulseTraceFromParameters(params, traceLength)
% Create a trace representing a pulse, based on header parameters

% gs 2007 dec
% ---------------------------------------
% params.sampleRate
% params.amplitude
% params.offset
% params.squarePulseTrainNumber
% params.squarePulseTrainISI
% params.squarePulseTrainWidth
% params.squarePulseTrainDelay

sr = params.sampleRate; % for convenience

% start the template -- flat line of appropriate length
pulseTrace = 0 * (1 : traceLength * sr)';

% add the offset
pulseTrace = pulseTrace + params.offset;

% make the steps
delayInd = params.squarePulseTrainDelay * sr + 1;
% note: add 1 because first sample is at time 0
durInd = params.squarePulseTrainWidth * sr;
isiInd = params.squarePulseTrainISI * sr;
try
    if params.squarePulseTrainNumber == 1
        pulseTrace(delayInd : delayInd + durInd) = ...
            pulseTrace(delayInd : delayInd + durInd) + params.amplitude;
    elseif params.squarePulseTrainNumber > 1
        %         disp('quickViewTrace: multiple steps not yet implemented');
        for n = 0 : params.squarePulseTrainNumber - 1
            try
                startInd = delayInd + n * isiInd;
                endInd = startInd + durInd;

                if endInd <= size(pulseTrace, 1)
                    % normal step
                    pulseTrace(startInd : endInd) = ...
                        pulseTrace(startInd : endInd) + params.amplitude;

                elseif endInd > size(pulseTrace, 1)

                    if startInd <= size(pulseTrace, 1)
                        % truncated step in trace -- fill in to end
                        pulseTrace(startInd : end) = ...
                            pulseTrace(startInd : end) + params.amplitude;

                    elseif startInd > size(pulseTrace, 1)
                        % no step in trace -- nothing to do

                    end
                end
            catch
                'problem'
            end
        end
    end
catch
    disp('quickViewTrace: problem making pulse')
end
